package com.treasure.hunt.mine

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.webkit.WebResourceRequest
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.lifecycle.Observer
import com.module.lemlin.mode.ViewModeActivity
import com.module.lemlin.rxhttp.Status
import com.module.lemlin.rxhttp.msg
import com.treasure.hunt.R
import com.treasure.hunt.mode.OtherViewMode
import kotlinx.android.synthetic.main.activity_config.*
import rxhttp.wrapper.OkHttpCompat.url


class MineConfigActivity : ViewModeActivity<OtherViewMode>() {

    companion object {
        private const val EXTRA_KEY_TITLE = "EXTRA_KEY_TITLE"
        private const val EXTRA_KEY_NAME = "EXTRA_KEY_NAME"
        private const val EXTRA_KEY_URL = "EXTRA_KEY_URL"
        fun open(activity: Activity, title: String?, name: String?) {
            val intent: Intent = Intent(activity, MineConfigActivity::class.java).apply {
                putExtra(EXTRA_KEY_TITLE, title)
                putExtra(EXTRA_KEY_NAME, name)
            }
            activity.startActivity(intent)
        }

        fun openUrl(activity: Activity, title: String?, url: String?) {
            val intent: Intent = Intent(activity, MineConfigActivity::class.java).apply {
                putExtra(EXTRA_KEY_TITLE, title)
                putExtra(EXTRA_KEY_URL, url)
            }
            activity.startActivity(intent)
        }
    }

    override val layoutResId: Int
        get() = R.layout.activity_config

    override fun viewModel(): OtherViewMode {
        return createViewModel()
    }

    override fun stateBar(): StatusBar {
        return StatusBar(
            isLightMode = false,
            bottomColor = R.color.FF_020027
        )
    }

    override fun initView() {

        ivConfigBack.setOnClickListener {
            finish()
        }

        intent?.getStringExtra(EXTRA_KEY_TITLE)?.let {
            tvConfigTitle.text = it
        }

        wvConfigWeb.webViewClient = WebViewClient()
        wvConfigWeb.settings.apply {
            //支持缩放，默认为true。
            setSupportZoom(false)
            //调整图片至适合webview的大小
            useWideViewPort = true
            // 缩放至屏幕的大小
            loadWithOverviewMode = true
            //设置默认编码
            defaultTextEncodingName = "utf-8"
            //设置自动加载图片
            loadsImagesAutomatically = true
            //允许访问文件
            allowFileAccess = true
            //开启javascript
            javaScriptEnabled = true
            //支持通过JS打开新窗口
            javaScriptCanOpenWindowsAutomatically = true
            //支持内容重新布局
            layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN;
            //关闭webview中缓存
            cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK

        }
    }

    override fun initData() {
        intent?.getStringExtra(EXTRA_KEY_NAME)?.let {
            viewModel.otherGetConfig(it)
        }
        intent?.getStringExtra(EXTRA_KEY_URL)?.let {
            wvConfigWeb.webViewClient = object : WebViewClient() {
                override fun shouldOverrideUrlLoading(
                    view: WebView?,
                    request: WebResourceRequest?
                ): Boolean {
                    val uri = Uri.parse(it)
                    try {
                        if (!it.startsWith("http:") || !it.startsWith("https:")) {
                            val intent = Intent(
                                Intent.ACTION_VIEW,
                                Uri.parse(it)
                            )
                            startActivity(intent)
                            return true
                        }
                    } catch (e: Exception) {
                        return false
                    }

                    view!!.loadUrl(it)
                    return true
                }
            }
            wvConfigWeb.loadUrl(it)
        }
    }

    override fun initViewMode() {
        viewModel.otherConfigLiveData.observe(this, Observer { response ->
            if (response.status == Status.FAILURE) {
                toast(response.throwable?.msg ?: "未知错误")
            }
            if (response.status != Status.SUCCESS) {
                return@Observer
            }
            response.httpData?.data?.let {
                wvConfigWeb.loadData(it.content, ("text/html; charset=UTF-8"), (null))
            }
        })
    }
}